package client.model;

import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.LocateRegistry;
import common.*;
import client.client;

public class ClientModel{

	ClientImplementation cimple;
	ClientInterface stub;
	//Registry reg;
	int port = new java.util.Random().nextInt(1000)+12350;
	String host = "localhost";
	ServerInterface server;

	public ClientModel() throws Exception{
		cimple = new ClientImplementation(this);
		stub = (ClientInterface) UnicastRemoteObject.exportObject(cimple,0);
		Registry reg = LocateRegistry.createRegistry(port);
		reg.rebind(ClientInterface.NAME, stub);
	}

	void connect() throws Exception{
			Registry sreg = LocateRegistry.getRegistry(host, ServerInterface.PORT);
			server = (ServerInterface) sreg.lookup(ServerInterface.NAME);
	}
	
	public ServerInterface stub() throws Exception{
		if(server==null){ connect(); }
		return server;
	}
	
	public String getHost(){ return host; }
	public void setHost(String host){ this.host = host; }
	public ClientInterface getCI(){ return stub; }

	client c;
	public void setC(client c){
		this.c = c;
	}

	public void updateList(java.util.List<String> list){
		c.updateList(list);
	}
}